<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="文件归档" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="filing">
              <a-input placeholder="请输入文件归档" v-model="model.filing"/>
<!--                <a-radio-group v-model="model.filing">-->
<!--                <a-radio value="1">办公存档</a-radio>-->
<!--                <a-radio value="2">会议资料</a-radio>-->
<!--                <a-radio value="3">走访记录</a-radio>-->
<!--                </a-radio-group>-->
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="文件分类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fileType">
              <a-radio-group v-model="model.fileType">
                <a-radio value="1">音频</a-radio>
                <a-radio value="2">视频</a-radio>
                <a-radio value="3">文档</a-radio>
                <a-radio value="4">图片</a-radio>
                <a-radio value="5">其他</a-radio>
              </a-radio-group>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="文件上传" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input hidden v-model="model.id"/>
              <UploadBigFile @method="method"></UploadBigFile>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>

</template>

<script>

import { httpAction, getAction, postAction, deleteAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import UploadBigFile from '../../Upload'
import { message } from 'ant-design-vue';
import { ACCESS_TOKEN } from '@/store/mutation-types'
import Vue from 'vue'

export default {
  name: 'FileMainForm',
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {},
      url: {
        add: '/file/main/add',
        del: '/file/main/true/delete_md5',
        // edit: '/file/fileMain/edit',
        // queryById: '/file/fileMain/queryById'
      },
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    const ids_key = 'fileIds-' + Vue.ls.get(ACCESS_TOKEN);
    const info_key = 'fileInfo-' + Vue.ls.get(ACCESS_TOKEN);
    var fileInfo = localStorage.getItem(info_key);
    if(fileInfo != null){
      this.delete(fileInfo);
    }
    localStorage.removeItem(ids_key);
    localStorage.removeItem(info_key);
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    method(id){

      this.model.id = id;
    },
    delete(str){
      deleteAction(this.url.del, {str: str});
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          if(undefined === this.model.filing){
            message.warning('请选择文件归档！！！');
            return false;
          }
          if(undefined === this.model.fileType){
            message.warning('请选择文件分类！！！');
            return false;
          }
          // console.log('this.model.id==========', this.model.id);
          if(undefined === this.model.id){
            message.warning('请选择上传文件！！！');
            return false;
          }
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          // console.log(!this.model.id)
          // console.log(this.model.id)
          // if (!this.model.id) {
          //   httpurl += this.url.add
          //   method = 'post'
          // } else {
          //   httpurl += this.url.edit
          //   method = 'put'
          // }
          httpurl += this.url.add;
          method = 'post';
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              message.success(res.message);
              that.$emit('ok');
            } else {
              message.error(res.message);
            }
            localStorage.removeItem('fileIds-' + Vue.ls.get(ACCESS_TOKEN));
            localStorage.removeItem('fileInfo-' + Vue.ls.get(ACCESS_TOKEN));
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
  },
  components: {
    UploadBigFile
  }
}
</script>

<style>

</style>